from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
from django.shortcuts import render, redirect


# Create your views here.
from bookstore.models import BookInfo


def index(request):
    return HttpResponse("hello word")


def home(request):
    return HttpResponse("hello word 这里是项目模块子路由")


def show(request, age):
    return HttpResponse(str(age))


def user(request, name):
    return HttpResponse(name)


def path(request, path):
    return HttpResponse(path)


def phone(request, phone):
    return HttpResponse(phone)


def show_arg(request, num):
    return HttpResponse(num)


def show_args(request, num):
    return HttpResponse(num)


def name(request, name):
    # resquest常用属性
    print(request.GET)
    print(request.GET.get('username'))
    print(request.method)
    print(request.path)
    # 获取返回值列表
    return HttpResponse(name)


def handle_res(request):
    ''' res = HttpResponse('响应对象')
        res.content_type = 'text/html'
        res.status_code = 400
    '''
    # res = render(request,'example.html')
    # return JsonResponse({'id':1,'name':'qdd','age':18,'sex':'女'})  # 返回的参数是字典
    return  JsonResponse([1,2,3,4,5,6,7,8,9],safe=False)  # 返回的参数不是字典


def handle_redirect(request):
    # return HttpResponseRedirect('/book/home/')
    # redirect是HttpResponseRedirect快捷方式
    print(5 / 0)
    return redirect('/book/home/')


def tag(request):
    return render(request,'bookstore/template_demo01.html',context={'Title':'变量测试',
                                                                    'list':list(range(1,10))})


def tag2(request):
    li = [10,30,20,40]
    num = 9
    return render(request,'bookstore/tag2.html',locals())


def books(request):
    # 查询数据库
    books = BookInfo.objects.all()
    return render(request,'bookstore/books.html', context={'Title':'书本列表',
                                                           'books':books})